<template>
	<view class="" :class="ggclass">
		<view class="select-item" @click.capture="spread" :style="{padding:`0 ${padd}rpx`,width,height}">
			<view class="item"  v-if="params.name">
				<slot></slot><text>{{params.name}}</text>
				<text class="iconfont icon-xiajiantou mg_l10"
					:style="{transform: `rotate(${num}deg)`,transition: '0.5s'}">
				</text>
			</view>
			<view class="item" v-else>
				<slot></slot><text class="grey">{{hint}}</text>
				<text class="iconfont icon-xiajiantou mg_l10"
					:style="{transform: `rotate(${num}deg)`,transition: '0.5s'}">
				</text>
			</view>
		</view>
		<uni-popup ref="popRef" :round="50" @close="close" :safe-area="false" @change="changePop">
			<view class="picker-view-box">
				<view class="picker-title">
					<text class="" @click="close">取消</text>
					<view class="font_s32 font-weight">{{title}}</view>
					<text class="blue" @click="submit">确定 </text>
				</view>
				<picker-view v-if="visible" :indicator-style="indicatorStyle" :value="values" @change="bindChange"
					class="picker-view">
					<picker-view-column>
						<view class="item" v-for="(item,index) in list" :key="index">{{item.name}}</view>
					</picker-view-column>
				</picker-view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import {
		dateFilter as filter
	} from '../ways/filter.js'
	const date = new Date()
	export default {
		name:'c-picker-view-slot',
		inject: {
			Cinput: {
				default: {}
			}
		},
		props: {
			title: {
				type: String,
				default: '自定义'
			},
			disabled: {
				type: Boolean,
				default: false
			},
			value: {
				type: [Number,String],
				default:''
			},
			range_key: {
				type: String,
				default: 'name'
			},
			range_value: {
				type: String,
				default: 'id'
			},
			immediate: {
				type: Boolean,
				default: false
			},
			hint: {
				type: String,
				default: '请选择'
			},
			list:{
				type: Array,
				default: () => []
			},
		},
		data() {
			return {
				values: [],
				visible: false,
				indicatorStyle: `height: 50px;`,
				params:{},
				slideParams:{},
				num: 0,
			}
		},
		computed:{
			val(){
				return this.value
			}
		},
		methods: {
			changePop(e){
				if(e.show){
					this.num = 180
				}else{
					this.num = 0
				}
			},
			spread() {
				if (this.disabled) return
				this.$refs.popRef.open('bottom')
				this.visible= true
				this.setValues()
			},
			show() {
				this.visible = true
				this.$refs.popRef.open('bottom')
				this.setValues()
			},
			close(){
				this.$refs.popRef.close()
				this.visible = false
			},
			setValues() {		
				let index = this.list.findIndex(v=>v===this.val)
				this.values = [index>=0?index:0]
				this.bindChange({detail:{value:this.values}})
			},
			bindChange(e) {
				const val = e.detail.value
				this.slideParams.name =this.list[val][this.range_key]
				this.slideParams.id = this.list[val][this.range_value]
			},
			submit() {
				this.close()
				uni.showLoading()
				this.params = {...this.slideParams}
				let time = setTimeout(() => {
					this.$emit('change',this.params)
					this.$emit('input',this.params.id)
					this.$emit('update:name',this.params.name)
					uni.hideLoading()
					clearTimeout(time)
					this.$nextTick(() => {
						typeof this.Cinput.verify === 'function' && this.Cinput.verify()
					})
				}, 1000)
			},
		}
	}
</script>
<style lang="scss" scoped>
	.picker-view-box {
		position: relative;
		z-index: 1000;
		background-color: #fff;
		border-radius: 32rpx 32rpx 0 0;
	}

	.picker-title {
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 30rpx;
	}

	.picker-view {
		width: 750rpx;
		height: 500rpx;
		// margin-top: 20rpx;
	}

	.item {
		height: 80rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		text-align: center;
	}
	.select-item {
		// height: 70rpx;
		width: 100%;
		display: flex;
		justify-content: space-between;
		align-items: center;
		border-radius: 10rpx;
	}
	
	.icon-xiajiantou {
		display: inline-block;
	}
</style>
